package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

/**
 * @description: 生成client_token
 * @author: Hupengpeng
 * @time: 2021/7/3 10:51
 */
public class OauthClientToken {
    private String client_key;
    private String client_secret;
    private String grant_type;
    private String response;
    private OauthClientToken() {
    }

    public String getClient_key() {
        return client_key;
    }

    public OauthClientToken setClient_key(String client_key) {
        this.client_key = client_key;
        return this;
    }

    public String getClient_secret() {
        return client_secret;
    }

    public OauthClientToken setClient_secret(String client_secret) {
        this.client_secret = client_secret;
        return this;
    }

    public String getGrant_type() {
        if (StrUtil.isEmpty(grant_type)) {
            grant_type = "client_credential";
        }
        return grant_type;
    }

    public OauthClientToken setGrant_type(String grant_type) {
        this.grant_type = grant_type;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public OauthClientToken setResponse(String response) {
        this.response = response;
        return this;
    }

    protected static OauthClientToken New() {
        return new OauthClientToken();
    }

    public OauthClientToken execute() {
        String url = StrUtil.format("{}{}?client_key={}&client_secret={}&grant_type={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_OAUTH_CLIENT_ACCESS_TOKEN,
                getClient_key(),
                getClient_secret(),
                getGrant_type());
        String rs = HttpUtil.get(url);
        response = rs;
        return this;
    }
    public OauthClientTokenResponse convert() {
        JSONObject obj = JSONUtil.parseObj(response);
        if ("success".equalsIgnoreCase(obj.getStr("message"))) {
            return JSONUtil.toBean(obj.getJSONObject("data"), OauthClientTokenResponse.class);
        }
        throw new DouYinException(response);
    }

    public class OauthClientTokenResponse{
        private String access_token;
        private String captcha;
        private String desc_url;
        private String description;
        private int error_code;
        private int expires_in;

        public String getAccess_token() {
            return access_token;
        }

        public OauthClientTokenResponse setAccess_token(String access_token) {
            this.access_token = access_token;
            return this;
        }

        public String getCaptcha() {
            return captcha;
        }

        public OauthClientTokenResponse setCaptcha(String captcha) {
            this.captcha = captcha;
            return this;
        }

        public String getDesc_url() {
            return desc_url;
        }

        public OauthClientTokenResponse setDesc_url(String desc_url) {
            this.desc_url = desc_url;
            return this;
        }

        public String getDescription() {
            return description;
        }

        public OauthClientTokenResponse setDescription(String description) {
            this.description = description;
            return this;
        }

        public int getError_code() {
            return error_code;
        }

        public OauthClientTokenResponse setError_code(int error_code) {
            this.error_code = error_code;
            return this;
        }

        public int getExpires_in() {
            return expires_in;
        }

        public OauthClientTokenResponse setExpires_in(int expires_in) {
            this.expires_in = expires_in;
            return this;
        }
    }
}
